package com.xb.loan.usercenter.user.user.service.impl;

import com.xb.loan.usercenter.service.user.IUserThresholdService;
import com.xb.loan.usercenter.service.user.bean.UserThresholdVO;
import com.xb.loan.usercenter.service.user.constant.UserConst;
import com.xb.loan.usercenter.user.user.dao.UserThresholdDao;
import com.xb.loan.usercenter.user.user.dao.bean.UserThreshold;
import com.xb.loan.util.cache.DefaultCacheFactory;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.common.ObjectOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户提现充值阈值ServiceImpl
 */
@Service("userThresholdService")
public class UserThresholdServiceImpl implements IUserThresholdService {
	
	@Autowired
	private UserThresholdDao userThresholdDao;

    @Override
    public int insert(UserThresholdVO record) {
        return insert(record,true);
    }

    @Override
    public int insert(UserThresholdVO userThresholdVO, boolean isEnableCache) {
    	String id = FormatOperation.assemblySeq(UserConst.UserSeq.COMMONUSER.getName(),
                UserConst.UserSeq.COMMONUSER.getCode());

    	userThresholdVO.setId(id);
    	
    	UserThreshold userThreshold = ObjectOperation.convertByClasses(userThresholdVO, UserThreshold.class);
        int rowCount = userThresholdDao.insertUserThreshold(userThreshold);
        
        if (rowCount > 0 && isEnableCache) {
            DefaultCacheFactory.getFacotry().getCache().setBean(UserThresholdVO.class,
            		userThresholdVO.getPartnerId(), userThresholdVO);
        }
        return rowCount;
    }

    @Override
    public int deleteById(String id) {
        return deleteById(id,true);
    }


    @Override
    public int deleteById(String id, boolean isEnableCache) {
        UserThreshold ut = userThresholdDao.queryUserThresholdById(id);
        
        int rowCount = userThresholdDao.deleteUserThresholdById(id);
        
        if (rowCount > 0 && isEnableCache) {
            DefaultCacheFactory.getFacotry().getCache().removeBean(UserThresholdVO.class, 
                    ut.getPartnerId());
        }
        return rowCount;
    }

    @Override
    public int updateById(String id, Double maxOrderMoney, Integer maxChargeNum,
                          Double maxChargeMoney, Integer maxEncashNum, Double maxEncashMoney,
                          Double encashThreshold, Integer status) {
        return updateById(id, maxOrderMoney, maxChargeNum, maxChargeMoney, 
        		maxEncashNum, maxEncashMoney, encashThreshold, status, true);
    }

    @Override
    public int updateById(String id, Double maxOrderMoney, Integer maxChargeNum,
                          Double maxChargeMoney, Integer maxEncashNum, Double maxEncashMoney,
                          Double encashThreshold, Integer status, boolean isEnableCache) {
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("id", id);
    	params.put("maxOrderMoney", maxOrderMoney);
    	params.put("maxChargeNum", maxChargeNum);
    	params.put("maxChargeMoney", maxChargeMoney);
    	params.put("maxEncashNum", maxEncashNum);
    	params.put("maxEncashMoney", maxEncashMoney);
    	params.put("encashThreshold", encashThreshold);
    	
        int rowCount= userThresholdDao.updateUserThreshold(params);
        
        if (rowCount > 0 && isEnableCache) {
        	UserThreshold userThreshold = userThresholdDao.queryUserThresholdById(id);
            DefaultCacheFactory.getFacotry().getCache().setBean(UserThresholdVO.class, 
                    userThreshold.getPartnerId(), ObjectOperation.convertByClasses(userThreshold, UserThresholdVO.class));
        }
        return rowCount;
    }

	@Override
	public List<UserThresholdVO> queryUserThreshold(Integer status, int pageNum, int numPerPage) {
		Map<String,Object> params=new HashMap<String,Object>();
        params.put("status", status);
        //params.put("partnerId", partnerId);
        
        int start = (pageNum - 1) * numPerPage + 1;
		int end = pageNum * numPerPage;
		params.put("start", start);
		params.put("end", end);
		
		List<UserThreshold> list = userThresholdDao.queryUserThreshold(params);
		return ObjectOperation.convertByClassesList(list, UserThresholdVO.class);
	}


    @Override
	public int queryUserThreshold(Integer status) {
		Map<String,Object> params=new HashMap<String,Object>();
        params.put("status", status);
		return userThresholdDao.queryUserThresholdCount(params);
	}

    /**
     * 原来业务是查询渠道，现在应该是查询公司帐号信息
     * @param id
     * @return
     */
    @Override
    public UserThresholdVO selectByKey(String id) {
        return null;
    }

}
